package neusoft.yuejian_11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.EventBean;
import neusoft.yuejian_11.bean.EventnBean;
import neusoft.yuejian_11.bean.GetInfo;
import neusoft.yuejian_11.bean.IsEventn;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.EventModel;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

public class EventnActivity extends BaseActivity implements RetrofitListener{
    protected Context context;
    private int activityId;
    private ImageButton imageButtonBack;
    private TextView textActivityName;
    private TextView textActivityTime;
    private TextView textActivityPrice;
    private TextView textUserName;
    private TextView textUserPhone;
    private Button buttonEventn;
    private ImageButton imageButtonFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eventn);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        textActivityName = (TextView) findViewById(R.id.text_activity_name);
        textActivityTime = (TextView) findViewById(R.id.text_activity_time);
        textActivityPrice = (TextView) findViewById(R.id.text_activity_price);
        textUserName = (TextView) findViewById(R.id.text_user_name);
        textUserPhone = (TextView) findViewById(R.id.text_user_phone);
        buttonEventn = (Button) findViewById(R.id.button_eventn);

        imageButtonFile = (ImageButton) findViewById(R.id.imageButtom_file);
    }

    private void initEvents() {
        imageButtonFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EventnActivity.this, InformationEActivity.class);
                intent.putExtra("a_id",activityId);
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        buttonEventn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventModel eventModel = new EventModel();
                eventModel.eventn(getU_id(),activityId,EventnActivity.this);
            }
        });
    }

    private void showData() {
        activityId = getIntent().getIntExtra("a_id",-1);
    }

    private void initData() {
        EventModel eventModel = new EventModel();
        eventModel.eventDetail(String.valueOf(activityId),this);

        EventModel eventModel1 = new EventModel();
        eventModel1.IsAddActivityn(getU_id(),String.valueOf(activityId),this);

        UserModel userModel = new UserModel();
        userModel.getUserInfo(getU_id(),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==1) {
            EventBean eventBean = (EventBean) o;
            textActivityName.setText(eventBean.getA_name());
            textActivityTime.setText(eventBean.getA_time());
            textActivityPrice.setText(String.valueOf(eventBean.getA_price()));
        }
        if (flag==2){
            GetInfo getInfo = (GetInfo) o;
            textUserName.setText(getInfo.getU_name());
            textUserPhone.setText(getInfo.getU_phone());
        }
        if (flag==5) {
            IsEventn isEventn = (IsEventn) o;
            if (isEventn.getAn_state().equals("已报名")) {
                Toast.makeText(EventnActivity.this, "该活动已报名", Toast.LENGTH_SHORT).show();
            }
        }
        if (flag==4){
            EventnBean eventnBean = (EventnBean) o;
            if (eventnBean.getSuccess().equals("1")) {
                    Toast.makeText(EventnActivity.this, "报名成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EventnActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
                }
        }
    }

    @Override
    public void onFail() {

    }
}
